﻿/// <reference path="../Scripts/jquery-1.8.1-vsdoc.js" />
/// <reference path="../Scripts/util.js" />
/// <reference path="../Scripts/Bonus.js" />


$.ajaxSetup({
    type: "POST",
    dataType: "text",
    global: false
});

var member;
var projectID;
var userID;
var empIDs;
function Init() {
    member = new Bonus(
        document.getElementById("parentMemberContainer"),
        document.getElementById("hdMemberCount"),
        document.getElementById("divNewMember_0"),
        "divNewMember_",
        "removeMeber_",
        "NoMember_",
        this.projectID,
        this.userID);
}

$(document).ready(function () {
    category = $("#MainContent_Category").val();
    projectID = $("#MainContent_ID").val();
    userID = $("#MainContent_UserID").val();
    Init();
    BindDepartment();
    $("#btnAddMember").bind("click", AddMember);
    $("#btnSave").bind("click", Back);
    $("#btnBack").bind("click", Back);
    $("#DepMember").change(function () {
        BindEmployee($("#DepMember").find('option:selected').val(), "#EmpMember");
    });
    this.empIDs = new Array();
    LoadMembers();
});

function Back() {
    Util.freshPage("BonusProjectManage.aspx?ID=" + projectID);
}

function BindDepartment() {
    $.ajax({
        url: "/Handler/Department.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadData" },
        success: function (data) {
            var ret = $.parseJSON(data).Departments;
            var depMember = $("#DepMember");
            depMember.empty();
            depMember.append("<option value='-1'>-请选择-</option>");
            for (var i = 0; i < ret.length; i++) {
                depMember.append("<option value='" + decodeURIComponent(ret[i].ID) + "'>" + decodeURIComponent(ret[i].Name) + "</option>");
            }
        }
    });
}

function BindEmployee(depID, emp) {
    $.ajax({
        url: "/Handler/Employee.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadDataByDepID", depID: depID },
        success: function (data) {
            var ret = $.parseJSON(data).Employees;
            var employees = $(emp);
            employees.empty();
            employees.append("<option value='-1'>-请选择-</option>");
            for (var i = 0; i < ret.length; i++) {
                var value = decodeURIComponent(ret[i].EmpID) + "|" + decodeURIComponent(ret[i].EmpName) + "|" + decodeURIComponent(ret[i].DepName)
                employees.append("<option value='" + value + "'>" + decodeURIComponent(ret[i].EmpName) + "</option>");
            }
        }
    });
}

function LoadMembers() {
    $.ajax({
        url: "/Handler/BonusProjectDetail.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadData", ProjectID: projectID },
        beforeSend: function () {
            Util.blockUI("正在加载数据...");
        },
        success: function (data) {
            Util.unblockUI();
            var ret = $.parseJSON(data).Bonus;
            for (var i = 0; i < ret.length; i++) {
                var memberInfo = ret[i];
                member.addMember(decodeURIComponent(memberInfo.EmpID), decodeURIComponent(memberInfo.EmpName), decodeURIComponent(memberInfo.DepName), decodeURIComponent(memberInfo.Month), decodeURIComponent(memberInfo.Amount));
            }
        }
    });
}

function AddMember() {
    var emp = $("#EmpMember").find('option:selected').val();
    if (emp == undefined) {
        return;
    }

    var arr = emp.split('|');
    if (arr[0] != -1) {
        var ret = member.addMember(arr[0], arr[1], arr[2], $("#Month").val(), $("#Amount").val());
        if (!ret) {
            alert('记录已经添加，请确认!');
            return;
        }
        AddProjectMember(arr[0]);
    }
}

function AddProjectMember(empID) {
    var postData =
    {
        "ProjectID": encodeURIComponent(projectID),
        "EmployeeID": encodeURIComponent(empID),
        "Month": encodeURIComponent($("#Month").val()),
        "Amount": encodeURIComponent($("#Amount").val()),
        "LastModifiedBy": encodeURIComponent(userID)
    };
    var jsonStr = $.toJSON(postData);
    $.ajax({
        url: "/Handler/BonusProjectDetail.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "addData", jsonDataString: jsonStr },
        success: function (data) {
        }
    });
}